from django.conf import settings
from django.contrib.sites.models import Site
from django.views.generic import date_based, list_detail
from models import Story, Section

def archive_index(request, section):
    qs = Story.published_objects.filter(assoc_section__site=settings.SITE_ID, 
	                                assoc_section__slug=section)
    section = Section.objects.get(site = settings.SITE_ID,
                                slug = section)
    site = Site.objects.get(id=settings.SITE_ID)
    return date_based.archive_index(request, queryset = qs, 
	                            date_field = 'pub_date',
				    extra_context={'section': section, 'site': site,})

def archive_year(request, section, year):
    qs = Story.published_objects.filter(assoc_section__site=settings.SITE_ID, 
	                                assoc_section__slug=section)
    section = Section.objects.get(site = settings.SITE_ID,
                                slug = section)
    site = Site.objects.get(id=settings.SITE_ID)
    return date_based.archive_year(request, queryset = qs, year = year,
	                           date_field = 'pub_date', make_object_list = True,
				   extra_context={'section': section, 'site': site,})

def archive_month(request, section, year, month):
    qs = Story.published_objects.filter(assoc_section__site=settings.SITE_ID, 
	                                assoc_section__slug=section)
    section = Section.objects.get(site = settings.SITE_ID,
                                slug = section)
    site = Site.objects.get(id=settings.SITE_ID)
    return date_based.archive_month(request, queryset = qs, year = year,
	                            month = month, date_field = 'pub_date',
				    extra_context={'section': section, 'site': site,})

def archive_day(request, section, year, month, day):
    qs = Story.published_objects.filter(assoc_section__site=settings.SITE_ID, 
	                                assoc_section__slug=section)
    section = Section.objects.get(site = settings.SITE_ID,
                                slug = section)
    site = Site.objects.get(id=settings.SITE_ID)
    return date_based.archive_day(request, queryset = qs, year = year,
	                          month = month, day = day,
				  date_field = 'pub_date',
				  extra_context={'section': section, 'site': site,})
    
def archive_today(request, section):
    qs = Story.published_objects.filter(assoc_section__site=settings.SITE_ID, 
	                                assoc_section__slug=section)
    section = Section.objects.get(site = settings.SITE_ID,
                                slug = section)
    site = Site.objects.get(id=settings.SITE_ID)
    return date_based.archive_today(request, queryset = qs, 
	                            date_field = 'pub_date',
				    extra_context={'section': section, 'site': site,})

def object_detail(request, section, slug, year, month, day):
    qs = Story.published_objects.filter(assoc_section__site=settings.SITE_ID, 
	                                assoc_section__slug=section)
    section = Section.objects.get(site = settings.SITE_ID,
                                slug = section)
    site = Site.objects.get(id=settings.SITE_ID)
    return list_detail.object_detail(request, queryset = qs, slug = slug,
				     extra_context={'section': section, 'site': site,})
